package main

import (
	"fmt"
	"gitee.com/wangqiOvO/mapper-go-sdk/pkg/service"
	"github.com/stianeikeland/go-rpio"
)

type GPIO struct {
	pin int
	modbusProtocolConfig GPIOProtocolConfig
	protocolCommonConfig GPIOProtocolCommonConfig
	visitorConfig GPIOVisitorConfig
}

type GPIOVisitorConfig struct {

}
type GPIOProtocolCommonConfig struct {

}
type GPIOProtocolConfig struct {

}

func (gpio *GPIO) SetConfig(protocolCommon,visitor,protocol []byte) (err error){
	gpio.SetPin()
	return nil
}

// ReadDeviceData  is an interface that reads data from a specific device, data is a type of string
func (gpio *GPIO) ReadDeviceData () (data interface{}, err error){
	pin := rpio.Pin(gpio.pin)
	if pin.Read() == 0{
		return "OFF",nil
	}else{
		return "ON",nil
	}
}

// WriteDeviceData is an interface that write data to a specific device, data's DataType is Consistent with configmap
func (gpio *GPIO) WriteDeviceData (data interface{}) (err error){
	status := data.(string)
	pin := rpio.Pin(gpio.pin)
	if status == "OFF"{
		pin.Output()
		pin.Low()
	}else{
		pin.Output()
		pin.Low()
	}
	return nil
}

// StopDevice is an interface to disconnect a specific device
func (gpio *GPIO) StopDevice() (err error){
	fmt.Println("----------Stop Virtual Device Successful----------")
	return nil
}



// GetDeviceStatus is an interface to get the device status true is OK , false is DISCONNECTED
func (gpio *GPIO) GetDeviceStatus() (status bool){
	return true
}


func (gpio *GPIO)SetPin(){
	if gpio.pin == 0{
		gpio.pin = 18
	}else{
		return
	}
}


func main() {
	gpio := &GPIO{}
	service.Main("gpioTest",gpio)
}

